'use strict';
import { message } from 'antd';

export const Msg = {
	MsgErrList: [],
	MsgSuccessList: [],
	MsgWarningList: [],
	error: function(massage, duration, onClose) {
		this.showMsg('error', massage, duration, onClose);
	},
	success: function(massage, duration, onClose) {
		this.showMsg('success', massage, duration, onClose);
	},
	warning: function(massage, duration, onClose) {
		this.showMsg('warning', massage, duration, onClose);
	},
	showMsg: function(type, massage, duration, onClose) {
		const key = this.getMsgKey(type);
		if(!key) return;
		let MsgList = this[key];
		duration = duration?duration : 3;
		if(MsgList.indexOf(massage) === -1){
			switch(type) {
				case 'error':
	    		message.error(massage, duration, onClose);
					break;
				case 'success':
	    		message.success(massage, duration, onClose);
					break;
				case 'warning':
	    		message.warning(massage, duration, onClose);
					break;
				default:
					console.log('只能展示正确,警告或者错误信息');
					return;
			}
			this[key].push(massage);
    	setTimeout(() => {
	    	this[key] = this[key].filter((item) => item !== massage);
	    },duration?duration * 1000 : 3000);
	
		}
	},
	getMsgKey: function(type) {
		switch(type) {
			case 'error':
				return 'MsgErrList';
			case 'success':
				return 'MsgSuccessList';
			case 'warning':
				return 'MsgWarningList';
			default:
				console.log('只能展示正确或者错误信息');
				return;
		}
	}
};
